package com.ojoin.trade.t0loan_core.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.SMSHelper;
import com.ojoin.trade.common.utils.json.JSONUtils;
import com.ojoin.trade.common.web.controller.SimpleBaseController;
import com.ojoin.trade.common.web.domain.BankCardBin;
import com.ojoin.trade.common.web.domain.Result;
import com.ojoin.trade.common.web.domain.Result.Status;
import com.ojoin.trade.common.web.service.BankCardBinService;
import com.ojoin.trade.common_core.utils.TradeUtils;
import com.ojoin.trade.common_core.web.domain.TbBranch;
import com.ojoin.trade.common_core.web.service.BranchBaseService;
import com.ojoin.trade.common_core.web.service.CardBaseService;
import com.ojoin.trade.t0loan_core.web.domain.BankCard;

@Controller
@RequestMapping("/card")
public class CardController  extends SimpleBaseController {

	private Logger log = LoggerFactory.getLogger(CardController.class);
	@Autowired
	private CardBaseService cardbaseservice;
	
	@Autowired
	private	BankCardBinService  bankCardBinService;
	
	@Autowired
	private BranchBaseService branchservice;
	
	public CardBaseService getCardbaseservice() {
		return cardbaseservice;
	}

	@ResponseBody
	@RequestMapping(value = "/bindcard", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result bindCard(HttpServletRequest request,
			 String mobile,  String cardNo,
			 String cardProvince,  String cardCity,
			 String branchNo,
			 String mobileVerifyCode) {
		String mhtNo = TradeUtils.getMhtNo(request);
		if (StringUtils.isEmpty(mobile)) {
			return new Result(Status.ERROR, "请填写银行预留手机号");
		}
		if (StringUtils.isEmpty(cardNo)) {
			return new Result(Status.ERROR, "请填写银行卡号");
		}
		if (StringUtils.isEmpty(cardProvince)) {
			return new Result(Status.ERROR, "请填写所属省");
		}
		if (StringUtils.isEmpty(cardCity)) {
			return new Result(Status.ERROR, "请填写所属市");
		}
		if (StringUtils.isEmpty(branchNo)) {
			return new Result(Status.ERROR, "请填写银行分支号");
		}
		try {
			List list = getCardbaseservice().bindCard(mhtNo, mobile, cardNo,
					cardProvince, cardCity, branchNo);
			Map<String, String> data = (Map) list.get(1);
			
			//确保验证码正确
			boolean isValidShortMsgCode=SMSHelper.validateAddBankCardCode(mhtNo, mobileVerifyCode);
			if (CommonUtils.isSuccess(list)&&isValidShortMsgCode) {
				SMSHelper.clearAddBankCardValidateCode(mhtNo);
				return new Result(Status.OK, data);
			} else {
				return CommonUtils.buildError(list);
			}
		} catch (Exception e) {
			log.error("绑卡失败", e);
			return new Result(Status.ERROR, "绑卡失败，请重试");
		}
	}

	@ResponseBody
	@RequestMapping(value = "/unbind", method = RequestMethod.POST)
	public Result unbindCard(HttpServletRequest request,
			 String cardId) {
		String mhtNo = TradeUtils.getMhtNo(request);
		if (StringUtils.isEmpty(cardId)) {
			return new Result(Status.ERROR, "无效卡号");
		}
		try {
			List list = getCardbaseservice().unBindCard(mhtNo, cardId);
			Map<String, String> data = (Map) list.get(1);
			if (CommonUtils.isSuccess(list)) {
				return new Result(Status.OK, list);
			} else {
				return CommonUtils.buildError(list);
			}
		} catch (Exception e) {
			log.error("解除卡绑定失败", e);
			return new Result(Status.ERROR, "解除卡绑定失败，请重试");
		}

	}

	@ResponseBody
	@RequestMapping(value = "queryBindCardList", method = RequestMethod.GET)
	public Result queryBindCardList(HttpServletRequest request) {
		String mhtNo = TradeUtils.getMhtNo(request);
		String cardNo= TradeUtils.getMerchUser(request).getCardNo();
		List list = getCardbaseservice().queryBindCardList(mhtNo);
		Map<String, String> data = (Map) list.get(1);
		if (CommonUtils.isSuccess(list)) {
			String bankName="其他银行";
			String cardstr = data.get("cardData");
			BankCard[] cards=JSONUtils.parseObject(cardstr, BankCard[].class);
			for(BankCard card:cards){
				//优先从分行查询
				TbBranch query=new TbBranch();
				query.setUnionBankNo(card.getBranchNo());
				TbBranch branchVO = branchservice.queryOne(query);
				if(null!=branchVO){
					bankName=branchVO.getBankName();
				}
				//再匹配CardBinDB
				if(StringUtils.isNotEmpty(bankName)){
					BankCardBin bcQuery=new BankCardBin();
					bcQuery.setDemo(card.getCardNo());
					BankCardBin bb=bankCardBinService.queryOne(bcQuery);
					if(null!=bb){
						bankName=bb.getBankName();
					}
				}
				
				card.setBankName(bankName);
				card.setPulbicAccountNo(card.getCardNo().equals(cardNo));
			}
			
			return new Result(Status.OK, cards);
		} else {
			if("商户账户无效".equals(CommonUtils.buildError(list).getMessage())){
				return new Result(Status.OK,"[]");
			}else{
				return CommonUtils.buildError(list);
			}
		}
	}
}
